package com.dxx.D201910312055.dxx;

import android.accessibilityservice.GestureDescription;
import android.graphics.Path;
import android.os.Build;
import android.view.accessibility.AccessibilityNodeInfo;

import com.dxx.D201910312055.dxx.utils.FindAccessibilityNodeInfosByClassName;

import static java.lang.System.out;


public class Utils {


    //不要修改这些值，不然方法可能逻辑错误。可以使用，千万不要在这里修改默认值
    private static boolean isPerformAction = false;


    /**
     * 打印所有 ClassName
     *
     * @param startTime 时间后执行，在此之前请打开到要获取的界面上
     * @param NodeInfo  遍历的最上层
     */
    public static void printlnClassName(final int startTime, final AccessibilityNodeInfo NodeInfo) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(startTime);
                    if (NodeInfo == null) {
                        return;
                    }
                    out.println(NodeInfo.getClassName());

                    int childCount = NodeInfo.getChildCount();
                    //遍历
                    for (int i = 0; i < childCount; i++) {
                        AccessibilityNodeInfo childNodeInfo = NodeInfo.getChild(i);
                        printlnClassName(0, childNodeInfo);

                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    /**
     * 查找指定控件操作,是多个控件的.针对复杂控件布局保留
     *
     * @param nodeInfo  父 递规则
     * @param ClassName 参考：AccessibilityNodeInfo.getClassName
     * @param Action    参考：AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD
     * @return 查找是否成功，不是执行成功
     */
    public static boolean mPerformAction(final AccessibilityNodeInfo nodeInfo, final String ClassName, int Action) {
        isPerformAction = false;

        if (nodeInfo == null) {
            return isPerformAction;
        }
        int childCount = nodeInfo.getChildCount();


        if (ClassName.equals(nodeInfo.getClassName())) {

            nodeInfo.performAction(Action);

            //因为是递规，经管可能最后不是返回这个，但是这里还是要中断的

            nodeInfo.refresh();

            isPerformAction = true;
            return isPerformAction;


        }


        //遍历
        for (int i = 0; i < childCount; i++) {

            AccessibilityNodeInfo childNodeInfo = nodeInfo.getChild(i);
            mPerformAction(childNodeInfo, ClassName, Action);

        }


        return isPerformAction;
    }


    /**
     * 滑动 安卓7.0
     * xy:两点直线滑动
     * 不允许同时执行，不然取消前面的操作
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @param startTime 0即可执行
     * @param duration  滑动时长
     * @return 执行是否成功
     */
    public static boolean mDispatchGesture(final int x1, final int y1, final int x2, final int y2, final int startTime, final int duration) {
        final boolean[] isDispatchGesture = {false};

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Path mPath = new Path();//线性的path代表手势路径,点代表按下,封闭的没用


            mPath.moveTo(x1, y1);
            mPath.lineTo(x2, y2);

            final GestureDescription.StrokeDescription sd2 = new GestureDescription.StrokeDescription(mPath, startTime, duration);

            isDispatchGesture[0] = AutoService.mService.dispatchGesture(new GestureDescription.Builder().addStroke(sd2).build(), null, null);


        }


        return isDispatchGesture[0];
    }

}
